package nets

import (
	"time"

	"github.com/gin-gonic/gin"

	"api/service"
)

func selectActivity(c *gin.Context) {
	var m service.Message
	m.SelectActivitys()
	c.JSON(200, m.Result)
}

func finishActivity(c *gin.Context) {
	type FinishInfo struct {
		Token      string `json:"token"`
		ActivityId int    `json:"activity_id"`
	}
	var getData FinishInfo
	if err := c.ShouldBindJSON(&getData); err != nil {
		c.JSON(400, gin.H{"error": err.Error()})
		return
	}
	isOk, htTeacher, _ := authenticationInElectron(getData.Token, c)
	if !isOk {
		c.JSON(400, gin.H{"error": "验证错误"})
		return
	}
	if !htTeacher {
		c.JSON(400, gin.H{"message": "只有管理员才能删除会员卡"})
		return
	}
	var m service.Message
	m.FinishActivity(getData.ActivityId)
	returnMdotInfoOrResult(m, c)

}

func insertActivity(c *gin.Context) {
	type InsertInfo struct {
		Token   string    `json:"token"`
		Title   string    `json:"title"`
		Content string    `json:"content"`
		Picture string    `json:"picture"`
		Link    string    `json:"link"`
		EndTime time.Time `json:"endTime"`
	}
	var getData InsertInfo
	if err := c.ShouldBindJSON(&getData); err != nil {
		c.JSON(400, gin.H{"error": err.Error()})
		return
	}
	isOk, htTeacher, _ := authenticationInElectron(getData.Token, c)
	if !isOk {
		c.JSON(400, gin.H{"error": "验证错误"})
		return
	}
	if !htTeacher {
		c.JSON(400, gin.H{"message": "只有管理员才能添加新活动"})
		return
	}
	var m service.Message
	m.InsertActivity(getData.Title, getData.Content, getData.Picture, getData.Link, getData.EndTime)
	returnMdotInfoOrResult(m, c)
}
